package gruppe7.servlets;


import gruppe7.data.UserController;
import gruppe7.tlrs.Station;
import gruppe7.tlrs.TLRSControl;
import gruppe7.util.Html;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class EditStationInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Html htmlInclude = new Html();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		
		PrintWriter out = response.getWriter();
		RequestDispatcher rd = request.getRequestDispatcher ("main_links.jsp"); 
		UserController uc = UserController.getInstance();
		
		out.println(htmlInclude.setHeader("Edit station"));
	    rd.include (request, response);
	    
	    out.println("<div id ='content'>");
	    out.println("<div id='content-left'>");
	    out.println("<h3><img src='images/edit.png' /> Edit Station</h3><br/>");
	   
	    if(request.getAttribute("message")!=null)
	    {
	    	String s = (String) request.getAttribute("message");
	    	out.println(s +"<br />");
	    }
	    	
	    if(uc.isValidSession(request))
	    {
	    	TLRSControl tickC = TLRSControl.getInstance();
			out.println("" +
					"<p><table class='default'>" +
					"<form action='EditStation' method='post'>" +
					"<tr><td>Choose station : </td>" +
					"<td><select name='station'>");
			for(Station s: tickC.getStations().values())
			{
			
				out.println("<option value='"+s.getName()+"'>"+s.getName()+"</option>");
			}
			
			out.println("</select></td></tr></br>" +
					"<tr><td></td><td align='right'><input type='submit' value='Edit' class='submit'/></td></tr>");
			out.println("</table>");
	    
	    }
	    
	    out.println("</div></div>");
		out.println(htmlInclude.setFooter());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		if(request.getParameter("editedName")!=null && request.getParameter("editedZone")!=null)
		{
			String stationName = (String) request.getParameter("editedName");
			String oldName = (String) request.getParameter("oldName");
			String zoneString = (String) request.getParameter("editedZone");
			String manager = (String) request.getParameter("manager");
			String comment = (String) request.getParameter("comment");
			String date = (String) request.getParameter("erectiondate");
			int zone = Integer.parseInt(zoneString);
			
			if(TLRSControl.getInstance().updateStation(oldName, stationName, zone, manager, date, comment))
			{
				request.setAttribute("message", "<p class='green'>You have updated the information about the Station </p>");
			}
			else
			{
				request.setAttribute("message", "<p class='red'>error: It didn't work!</p'>");
			}
		}
		this.doGet(request, response);
	}

}
